package com.tjtt.tdm.system.dao.impl;

import java.util.HashMap;
import java.util.List;

import com.tjtt.tdm.base.HibernateEntityDAO;
import com.tjtt.tdm.exception.DaoException;
import com.tjtt.tdm.system.bean.Menu;
import com.tjtt.tdm.system.dao.MenuDao;
import com.tjtt.tdm.system.dao.PipeDao;

public class PipeDaoImpl extends HibernateEntityDAO<Menu> implements PipeDao {
	public List<Menu> findMenuByParentId(Long parentid) throws DaoException {
		String hql = "from Menu as entity where entity.parentid=? order by entity.sequence";
		return this.findByHql(hql,parentid);
	}
	public long getMenuSequenceByParentId(Long parentId) throws DaoException {
		String sql = "select nvl(max(sequence),0) from sys_menu where 1=1 ";
		HashMap<String, Object> params = new HashMap<String,Object>();
		params.put("parentid",parentId);
		return this.getTotalCountBySQL(params, sql, "");
	}

	public void updateUpMenusPosition(Long parentId, Long seq)
			throws DaoException {
		String sql = "update Menu set sequence=sequence-1 where sequence>? and parentid=?";
		this.bulkUpdate(sql, new Long[]{seq,parentId});
	}
	
	public List<Menu> findMenuByRoleId(Long parentId, String roleId) throws DaoException {
		String hql = "from Menu as entity where entity.menuid in (select rolemenu.menuid from Rolemenu as rolemenu where rolemenu.roleid in("+roleId+") ) and entity.parentid=? order by entity.sequence asc";
		return super.findByHql(hql, parentId);
	}
}
